package com.glacier.thread;

import com.glacier.thread.Stock;
import lombok.*;

import java.util.concurrent.Callable;

/**
 * date 2024-01-03 09:40
 * 方法三：实现Callable接口
 *
 * @author glacier
 * @version 1.0
 */
@Data
@ToString
@EqualsAndHashCode(callSuper = false)
@RequiredArgsConstructor
public class MyCallable implements Callable<Integer> {

    private final Stock stock;

    @SneakyThrows
    @Override
    public Integer call() throws Exception {
        Thread.sleep(100);
        if (stock.getCount() > 0) {
            stock.decrement();
            System.out.println(Thread.currentThread().getName() + " count = " + stock.getCount());
        }
        return stock.getCount();
    }
}
